<?php
/**
 * Magento
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Open Software License (OSL 3.0)
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/osl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magentocommerce.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Magento to newer
 * versions in the future. If you wish to customize Magento for your
 * needs please refer to http://www.magentocommerce.com for more information.
 *
 * @category    Mage
 * @package     Mage_Review
 * @copyright   Copyright (c) 2011 Magento Inc. (http://www.magentocommerce.com)
 * @license     http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 */

/**
 * Review status
 *
 * @category   Mage
 * @package    Mage_Review
 * @author      Magento Core Team <core@magentocommerce.com>
 */

class Shangku_ReviewsWithImg_Model_Review_Image extends Mage_Catalog_Model_Product_Image
{

    public function __construct()
    {
        $this->_init('reviewswithimg/review_image');
    }
    
    public function getBaseMediaPath()
    {
    	return Mage::getBaseDir('media') . DS . 'review';
    }
    
    public function setBaseFile($file)
    {
    	$this->_isBaseFilePlaceholder = false;
    
    	if(is_null($file)){
    		$file = $this->getData('img_name');
    	}
    	if (($file) && (0 !== strpos($file, '/', 0))) {
    		$file = '/' . $file;
    	}
    	$baseDir = $this->getBaseMediaPath();
    
    	if ('/no_selection' == $file) {
    		$file = null;
    	}
    	if ($file) {
    		if ((!$this->_fileExists($baseDir . $file)) || !$this->_checkMemory($baseDir . $file)) {
    			$file = null;
    		}
    	}
    	if (!$file) {
    		// check if placeholder defined in config
    		$isConfigPlaceholder = Mage::getStoreConfig("catalog/placeholder/{$this->getDestinationSubdir()}_placeholder");
    		$configPlaceholder   = '/placeholder/' . $isConfigPlaceholder;
    		if ($isConfigPlaceholder && $this->_fileExists($baseDir . $configPlaceholder)) {
    			$file = $configPlaceholder;
    		}
    		else {
    			// replace file with skin or default skin placeholder
    			$skinBaseDir     = Mage::getDesign()->getSkinBaseDir();
    			$skinPlaceholder = "/images/catalog/product/placeholder/{$this->getDestinationSubdir()}.jpg";
    			$file = $skinPlaceholder;
    			if (file_exists($skinBaseDir . $file)) {
    				$baseDir = $skinBaseDir;
    			}
    			else {
    				$baseDir = Mage::getDesign()->getSkinBaseDir(array('_theme' => 'default'));
    				if (!file_exists($baseDir . $file)) {
    					$baseDir = Mage::getDesign()->getSkinBaseDir(array('_theme' => 'default', '_package' => 'base'));
    				}
    			}
    		}
    		$this->_isBaseFilePlaceholder = true;
    	}
    
    	$baseFile = $baseDir . $file;
    
    	if ((!$file) || (!file_exists($baseFile))) {
    		throw new Exception(Mage::helper('catalog')->__('Image file was not found.'));
    	}
    
    	$this->_baseFile = $baseFile;
    
    	// build new filename (most important params)
    	$path = array(
    			$this->getBaseMediaPath(),
    			'cache',
    			Mage::app()->getStore()->getId(),
    			$path[] = $this->getDestinationSubdir()
    	);
    	if((!empty($this->_width)) || (!empty($this->_height)))
    		$path[] = "{$this->_width}x{$this->_height}";
    
    	// add misk params as a hash
    	$miscParams = array(
    			($this->_keepAspectRatio  ? '' : 'non') . 'proportional',
    			($this->_keepFrame        ? '' : 'no')  . 'frame',
    			($this->_keepTransparency ? '' : 'no')  . 'transparency',
    			($this->_constrainOnly ? 'do' : 'not')  . 'constrainonly',
    			$this->_rgbToString($this->_backgroundColor),
    			'angle' . $this->_angle,
    			'quality' . $this->_quality
    	);
    
    	// if has watermark add watermark params to hash
    	if ($this->getWatermarkFile()) {
    		$miscParams[] = $this->getWatermarkFile();
    		$miscParams[] = $this->getWatermarkImageOpacity();
    		$miscParams[] = $this->getWatermarkPosition();
    		$miscParams[] = $this->getWatermarkWidth();
    		$miscParams[] = $this->getWatermarkHeigth();
    	}

    	$path[] = md5(implode('_', $miscParams));
    
    	// append prepared filename
    	$this->_newFile = implode('/', $path) . $file; // the $file contains heading slash
    
    	return $this;
    }
}
